<template>
  <div>
    <el-button type="primary" size="small" @click="toAdd">添加</el-button>
    <!-- 表格 -->
    <el-table size="small" :data="customers.filter(data => !search || data.realname.toLowerCase().includes(search.toLowerCase()))">
      
      <el-table-column prop="id" label="#" width="80" align="center" type="expand" >

      <template slot-scope="scope">
        <el-form label-position="left" inline class="demo-table-expand">
          <el-form-item label="用户头像" >
            <span>
              <el-avatar src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"></el-avatar>
            </span>
            </el-form-item>
          <el-form-item label="">
            <span>{{ }}</span>
          </el-form-item>
          </el-form-item>
          <el-form-item label="用户姓名">
            <span>{{ scope.row.realname }}</span>
          </el-form-item>
          <el-form-item label="注册时间">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{getTime(scope.row.registerTime)}}</span>
          </el-form-item>
          <el-form-item label="最后登录">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{getTime(scope.row.lastLoginTime)}}</span>
          </el-form-item>
          <el-form-item label="身份证号">
            <span>{{ scope.row.idCard }}</span>
          </el-form-item>
          <el-form-item label="银行卡号">
            <span>{{ scope.row.bankCard }}</span>
          </el-form-item>
        </el-form>
      </template>

      </el-table-column>
      
      <el-table-column prop="realname" label="用户名" width="150" />
      <el-table-column prop="telephone" label="手机号" />
      <el-table-column prop="registerTime" label="注册时间" >
        <template slot-scope="scope">
        <i class="el-icon-time"></i>
        <span style="margin-left: 10px">{{getTime(scope.row.registerTime)}}</span>
      </template>
      </el-table-column>

      <!-- <el-table-column prop="role.name" label="身份" width="100" /> -->
      <el-table-column
      prop="role.name"
      label="身份"
      width="100"
      :filters="[{ text: '员工', value: '员工' }, { text: '顾客', value: '顾客' }]"
      :filter-method="filterTag"
      filter-placement="bottom-end">
      <!-- <template slot-scope="scope">
        <el-tag
          :type="scope.row.role.name === '员工' ? 'primary' : 'success'"
          disable-transitions>{{scope.row.role.name }}</el-tag>
      </template> -->
    </el-table-column>

      <el-table-column prop="status" label="状态" width="100" />

      <el-table-column fixed="right">
        <template slot="header" slot-scope="scope">
        <el-input
          v-model="search"
          size="mini"
          placeholder="输入关键字搜索"/>
      </template>
        
        <template slot-scope="scope">
          <el-button type="danger" size="mini" @click="del(scope.row.id)">删除</el-button>
          <el-button type="primary" size="mini" @click="edit(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格结束 -->
    <!-- 模态框 -->
    <el-dialog title="用户" :visible.sync="visable">
      <el-form ref="ruleForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="realname">
          <el-input v-model="form.realname" clearable placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="手机号" prop="telephone">
          <el-input
            v-model="form.telephone"
            clearable
            placeholder="请输入手机号"
            maxlength="11"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" clearable placeholder="请输入密码" show-password />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="用户正常" value="用户正常" />
            <el-option label="用户禁用" value="用户禁用" />
          </el-select>
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-select v-model="form.roleId" placeholder="请选择角色">
            <el-option v-for="c in role" :key="c.id" :label="c.name" :value="c.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close('ruleForm')">取消</el-button>
        <el-button type="primary" @click="submit('ruleForm')">确定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>
<script>
import { get, post } from "@/utils/request";
export default {
  data() {
    return {
      text: '',
      search: '',
      visable: false,
      form: {},
      customers: [],
      rules: {
        realname: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        telephone: [
          { required: true, message: "请输入手机号", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
        roleId: [{ required: true, message: "请选择角色", trigger: "change" }]
      },
      role: [{ id: 1, name: '员工', value: 3 }, { id: 1, name: '顾客', value: 2 }]
    }
  },
  created() {
    this.loadUsers();
  },
  methods: {
    //获取时间
    getTime(time){
      return new Date(parseInt(time)).toLocaleString().replace(/:\d{1,2}$/,' ')
    },
    //筛选
    filterTag(value, row) {
        return row.role.name === value;
      },
    loadUsers() {
      let url = "http://47.92.81.116:8888/user/findAllWithRole";
      get(url).then(response => {
        this.customers = response.data;
      });
    },
    toAdd() {
      (this.form = {}), (this.visable = true);
    },
    del(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        // 删除
        let url = "http://47.92.81.116:8888/user/deleteById";
        get(url, { id: id }).then(response => {
          // 提示
          this.$message({ type: "success", message: response.message });
          // 刷新数据
          this.loadUsers();
        });
      });
    },
    submit(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          // 表单验证成功的情况
          let url = "http://47.92.81.116:8888/user/saveOrUpdate";
          post(url, this.form).then(response => {
            this.$message({ type: "success", message: response.message });
            this.visable = false;
            this.loadUsers();
            this.$refs[form].resetFields();
          });
        } else {
          return false;
        }
      });
    },
    close(form) {
      this.visable = false;
      this.$refs[form].resetFields();
    },
    edit(row) {
      this.form = row;
      this.visable = true;
    }
  }
};
</script>

<style>
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color:#99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 40%;
  }
</style>